﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooKeeperNet;

namespace DistributedLock.Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            var parent = "/root22/" + "plmi0075890rr" + "/123456";
            var result = ZKServer.CreateNodeTree(parent);
            for (int i = 0; i < 100000; i++)
            {
                Task.Factory.StartNew(() =>
                {
                    using (ZKServer server = new ZKServer(50000))
                    {
                        var node = server.CreateMetexNode(parent + "/create");
                        if (server.ExclusiveLock.TryEnter(node))
                        {
                            try
                            {
                                Console.WriteLine("成功了---" + node);
                            }
                            finally
                            {
                                Console.WriteLine(node + " has finished");
                                server.ExclusiveLock.Exit(node);
                            }
                        }
                        else
                        {
                            var children = ZKUtility.GetChildren(parent);
                            //Console.WriteLine("failed，children item count:" + children.Count);
                        }
                    }
                });
            }

            Console.ReadLine();
        }
    }
}
